package com.qfedu.edu.orderservice.slot.impl;

import com.qfedu.edu.constance.RedisConstance;
import com.qfedu.edu.exception.BusinessException;
import com.qfedu.edu.manager.RedisManager;
import com.qfedu.edu.orderservice.slot.generic.GenericSlot;
import com.qfedu.edu.result.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.digests.GeneralDigest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaobobo
 * @title: CheckStockCountSlot
 * @projectName seckill-fy-2401-parent
 * @description: 这个是检测库存是否充足的这个Slot
 * @date 2024/11/11  16:37
 */
@Component
@Slf4j
public class CheckStockCountSlot extends GenericSlot {

    @Autowired
    private RedisManager redisManager;

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE+1;
    }

    @Override
    public void dealRequest(HttpServletRequest req, HttpServletResponse resp) {
        //首先要拿到这个课程id
        //获取商品id
        String classId = req.getParameter("classId");
        //接下来构建这个key
        String key= RedisConstance.getProStockPrefix()+classId;
        //接下来获取Redis中的库存
        Integer realCount = (Integer) redisManager.getObject(key);
        //接下来再获取放行了多少人过去秒杀了...
        String keyCount=RedisConstance.getBuyCountPrefix()+classId;
        //获取这个数据
        Integer passNumber = (Integer) redisManager.getObject(keyCount);
        if(null==passNumber){
            //说明还没有记录
            //说明产品充足
            log.info("产品还没人购买，可以购买...");
            return;
        }
        if(realCount>passNumber){
            //说明产品充足
            log.info("产品充足，可以购买...");
            return;
        }
        log.info("产品已经秒杀完...请下次购买...");
        //执行到这里 说明产品秒杀完了...
        throw new BusinessException(ResponseCode.PRO_COUNT_OVER_EXCEPTION);
    }
}
